from collections import Counter

from db_conn.getBy import getCntByPYCG, getByProvinceAndYear, getCntByPYC
"""
生成条形统计图的数据处理层
"""

# def getRewardAndLevelService(province):
#     #初始化选项
#     category=['自然科学奖','技术发明奖','科学技术进步奖']
#     grade=['一等奖','二等奖','三等奖']
#     #初始化dict结构
#     dict={}
#     for year in range(2021,2024):
#         cateList=[]
#         for cate in category:
#             cntList = []  # 数量数组
#             formatted_data={}#项正确格式
#             for level in grade:
#                 cnt = getCntByPYCG(province, year, cate, level)
#                 cntList.append(cnt)
#                 formatted_data = {'name': cate, 'count': cntList}
#             cateList.append(formatted_data)
#         dict[year]=cateList
#     return dict
def getRewardAndLevelService(province):
    # 初始化选项
    categories = ['自然科学奖', '技术发明奖', '科学技术进步奖']
    grades = ['一等奖', '二等奖', '三等奖']

    # 初始化结果字典
    result_dict = {}

    # 循环遍历年份
    for year in range(2021, 2024):
        year_dict = {}

        # 循环遍历奖项级别
        for idx, level in enumerate(grades, start=1):
            count_list = []

            # 循环遍历奖项类别
            for category in categories:
                cnt = getCntByPYCG(province, year, category, level)  # 假设这个函数可以获取数量
                count_list.append(cnt)

            # 构建级别数据结构
            level_data = {'count': count_list, 'name': idx}

            # 将级别数据添加到年份字典中
            year_dict[idx] = level_data

        # 将年份字典添加到结果字典中
        result_dict[year] = year_dict

    return result_dict

# def getCompletionCntTop15(province,year):
#     data=getByProvinceAndYear(province, year)
#     completionList=[]
#     for dateunit in data:#数据单元
#         if dateunit[2]!=None:
#             coms=dateunit[2].split('、')
#             for com in coms:
#                 completionList.append(com)
#     wordCount = Counter(completionList)
#     # 前15个单位
#     top_15 = wordCount.most_common(15)
#     dict={}
#     dict['year']=year
#     dict['name']=[top[0] for top in top_15]
#     dict['count']=[top[1] for top in top_15]
#     return dict
#
# def getParticipateService(province):
#     data=[]
#     for year in range(2021,2024):
#         item=getCompletionCntTop15(province,year)
#         data.append(item)
#     return data



def getCompletionCntTop10(province, year):
    data = getByProvinceAndYear(province, year)
    completionList = []
    for dateunit in data:
        if dateunit[2] is not None:
            coms = dateunit[2].split('、')
            for com in coms:
                completionList.append(com)

    wordCount = Counter(completionList)
    top_10 = wordCount.most_common(10)

    result = {
        'name': [top[0] for top in top_10],
        'count': [top[1] for top in top_10]
    }
    return result

def getParticipateService(province):
    data = {}
    for year in range(2021, 2024):
        year_str = str(year)
        item = getCompletionCntTop10(province, year)
        data[year_str] = item
    return data
def getRewardCntList_Year(province,prize):
    list21To23=[]
    for year in range(2021,2024):
        cnt=getCntByPYC(province, year, prize)
        list21To23.append(cnt)
    return list21To23
def getRewardCntService(province):
    data=[]
    prize=['自然科学奖','技术发明奖','科学技术进步奖']
    for prizeItem in prize:
        dict={}
        dict['name']=prizeItem
        dict['count']=getRewardCntList_Year(province,prizeItem)
        data.append(dict)
    return data
if __name__ == '__main__':
    res=getRewardCntService('河北')
    print(res)